//
//  DrapLeftCell.swift
//  Game
//
//  Created by woniu on 2025/9/10.
//

import Foundation
import UIKit

class DrapLeftCell: UITableViewCell {
    public var onCheckFinishBlock: (()->())?
    private var container:UIView!
    private var imgCard:DrapImageCardView!
    private var placeCard:PlaceholderCardView!
    private let playBtn = UIButton(type: .system)
    private let resultIcon = UIImageView()
    private var wordItem:DrapWordItem?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        self.selectionStyle = .none
        backgroundColor = .black
        
        container = UIView()
        contentView.addSubview(container)
        container.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(6)
        }
        
        imgCard = DrapImageCardView()
        imgCard.circular(5)
        container.addSubview(imgCard)
        imgCard.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(150)
        }
        
        placeCard = PlaceholderCardView()
        container.addSubview(placeCard)
        placeCard.snp.makeConstraints { make in
            make.left.equalTo(imgCard.snp.right).offset(10);
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.width.equalTo(150)
        }
        
        playBtn.setImage(UIImage(named: "播放语音"), for: .normal)
        playBtn.tintColor = .white.withAlphaComponent(0.5)
        playBtn.isHidden = true
        playBtn.addTarget(self, action: #selector(onPlay), for: .touchUpInside)
        container.addSubview(playBtn)
        playBtn.snp.makeConstraints { make in
            make.left.equalTo(placeCard.snp.right).offset(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        resultIcon.contentMode = .scaleAspectFit
        container.addSubview(resultIcon)
        resultIcon.translatesAutoresizingMaskIntoConstraints = false
        resultIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width:30, height: 30))
            make.left.equalTo(playBtn.snp.right).offset(10)
            make.centerY.equalTo(playBtn.snp.centerY)
        }
        
    }
    
    @objc func onPlay(){
        let txt = placeCard.getWord()
        if !txt.isEmpty{
            Utils.play(word: txt)
        }
    }
    
    public func update(item: DrapWordItem,userSelecteWord:String?,index:Int) {
        self.wordItem = item
        imgCard.update(item: item)
        resultIcon.isHidden = !item.isShowResult
        playBtn.isHidden = !item.isShowResult
        placeCard.setWord(userSelecteWord)
        if item.isShowResult && item.didShowResult == false{
            item.didShowResult = true
            onCheckAndShow(index: index)
        }
    }
    
    private func onCheckAndShow(index:Int){
        let correct = placeCard.getWord() == imgCard.wordItem?.word
        self.wordItem?.isAnswerCorrect = correct
        resultIcon.image = correct ? UIImage(named: "正确") : UIImage(named: "错误")
        resultIcon.tintColor = correct ? .systemGreen : .systemRed
        
        resultIcon.isHidden = false
        resultIcon.alpha = 0
        playBtn.isHidden = false
        
        let delay = TimeInterval(index) * 0.20
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            UIView.animate(withDuration: 0.5, animations: {[weak self]in
                guard let `self` = self else { return }
                self.resultIcon.alpha = 1
                self.resultIcon.transform = .identity
                self.onCheckFinishBlock?()
            })
        }
    }
}
